import * as THREE from 'three'
import { EventBus } from '@/utils/EventBus'
export class myLight {
    constructor (scene) {
        this.scene = scene
        // 平行光的位置
        this.dirPosition = [
            [0, 5, 10],
            [-10, 5, 0],
            [0, 5, -10],
            [10, 5, 0]
        ]
        this.createCarDL()

        // 聚光灯
        this.nowSpotLight = {}

        // 聚光灯辅助器
        this.spotHelper = {}
        // 展厅
        this.nowSceneName = '展厅'

        // 创建聚光灯
        this.createSpotLight()


        EventBus.getInstance().on('changeSky', (sceneName) => {
            if (sceneName === this.nowSceneName) return
            if (sceneName === '户外') {
                this.removeSkyLight()
            } else if (sceneName === '展厅') {
                this.createSpotLight()
            }
            this.nowSceneName = sceneName
        })
    }

    // 创建照亮汽车的平行光
    createCarDL () {
        this.dirPosition.forEach(position => {
            const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5)
            // 设置四个方向的平行光
            directionalLight.position.set(...position)
            // 需添加到场景中
            this.scene.add(directionalLight)
        })

    }

    // 创建聚光灯
    createSpotLight () {
        // 设置聚光灯
        this.nowSpotLight = new THREE.SpotLight(0xffffff, 1)
        this.spotHelper = new THREE.SpotLightHelper(this.nowSpotLight, 0xffffff)
        this.scene.add(this.spotHelper)
        // 设置聚光灯照射范围的角度
        this.nowSpotLight.angle = 0.16 * Math.PI
        // 聚光锥边缘衰减程度(0 - 1)
        this.nowSpotLight.penumbra = 0.8
        // 开启阴影支持
        this.nowSpotLight.castShadow = true

        // 阴影贴图的大小
        this.nowSpotLight.shadow.mapSize.set(4096, 4096)
        this.nowSpotLight.position.set(0, 5, 0)
        this.scene.add(this.nowSpotLight)
    }

    removeSkyLight () {
        this.nowSpotLight.parent.remove(this.nowSpotLight)
        this.nowSpotLight = {} 
        this.spotHelper = {}
    }
}